package com.example.orchid.paper.activity;

import android.app.Fragment;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.example.orchid.paper.R;
import com.example.orchid.paper.fragment.NewsDetailImageFragment;
import com.example.orchid.paper.task.ImageToGalleryTask;
import com.example.orchid.paper.task.MyAsyncTask;

/**
 * 新闻详情页中图片，点击后展示Activity
 * Created by Orchid on 2015-10-15.
 */
public class NewsDetailImageActivity extends BaseActivity{
    private String mImageUrl = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        if (savedInstanceState == null) {
            mImageUrl = getIntent().getStringExtra("imageUrl");
        } else {
            mImageUrl = savedInstanceState.getString("imageUrl");
        }

        Bundle bundle = new Bundle();
        bundle.putString("imageUrl", mImageUrl);

        // Add the Sample Fragment if there is one
        Fragment newFragment = getFragment();
        newFragment.setArguments(bundle);

        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, newFragment).commit();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("imageUrl", mImageUrl);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected Fragment getFragment() {
        return new NewsDetailImageFragment();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.detail_image, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.action_first:
                new ImageToGalleryTask(this).executeOnExecutor(MyAsyncTask.THREAD_POOL_EXECUTOR, mImageUrl);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
